package com.liunian.algorithmstudy.backtracking.partition;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Partition131 {

	/**
	 给你一个字符串 s，请你将 s 分割成一些子串，使每个子串都是
	 回文串。返回 s 所有可能的分割方案。

	 示例 1：
	 输入：s = "aab"
	 输出：[["a","a","b"],["aa","b"]]

	 示例 2：
	 输入：s = "a"
	 输出：[["a"]]
	 */
	private List<List<String>> res = new ArrayList<>();
	private LinkedList<String> path = new LinkedList<>();
 	public List<List<String>> partition(String s) {
		backTracking(s, 0);
		return res;
	}

	private void backTracking(String s, int index) {
		if (index == s.length()) {
			res.add(new ArrayList<>(path));
			return;
		}
		for (int i = index; i < s.length(); i++) {
			if (isPalindrom(s, index, i + 1)) {
				path.add(s.substring(index, i + 1));
				backTracking(s, i + 1);
				path.removeLast();
			}
		}
	}

	private boolean isPalindrom(String s, int left, int right) {
		 while (left < right) {
			 if (s.charAt(left) != s.charAt(right - 1)) {
				 return false;
			 }
			 left++;
			 right--;
		 }
		return true;
	}

	public static void main(String[] args) {
		Partition131 partition = new Partition131();
		System.out.println(partition.partition("aab"));
	}


}
